﻿
// Test_StorageDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "Test_Storage.h"
#include "Test_StorageDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTestStorageDlg 对话框



CTestStorageDlg::CTestStorageDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_TEST_STORAGE_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestStorageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS1, m_EditIPAddr);
	DDX_Control(pDX, IDC_EDIT1, m_EditMsg);
}

BEGIN_MESSAGE_MAP(CTestStorageDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CTestStorageDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CTestStorageDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CTestStorageDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CTestStorageDlg::OnBnClickedButton4)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON5, &CTestStorageDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CTestStorageDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &CTestStorageDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &CTestStorageDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &CTestStorageDlg::OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON10, &CTestStorageDlg::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON11, &CTestStorageDlg::OnBnClickedButton11)
	ON_BN_CLICKED(IDC_BUTTON12, &CTestStorageDlg::OnBnClickedButton12)
END_MESSAGE_MAP()


// CTestStorageDlg 消息处理程序

BOOL CTestStorageDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_EditIPAddr.SetWindowText(_T("192.168.1.4"));
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2,2),&st_WSAData);
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CTestStorageDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CTestStorageDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CTestStorageDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString m_StrIPAddr;
	m_EditIPAddr.GetWindowText(m_StrIPAddr);
	
	if (!NetClient_TCPSelect_Create(m_StrIPAddr.GetBuffer(), 10331, &hSocket))
	{
		AfxMessageBox(_T("启动连接失败!"));
		return;
	}
}


void CTestStorageDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	NetClient_TCPSelect_Close(hSocket);
}

void CTestStorageDlg::Storage_MsgPrint()
{
	int nRcvLen = 2048;
	int nMsgLen = 2048;
	TCHAR tszRcvBuffer[2048];
	TCHAR tszMsgBuffer[2048];
	NETENGINE_PROTOCOLHDR st_ProtocolHdr;

	memset(tszRcvBuffer, '\0', sizeof(tszRcvBuffer));
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	memset(&st_ProtocolHdr, '\0', sizeof(NETENGINE_PROTOCOLHDR));

	NetClient_TCPSelect_RecvMsg(hSocket, tszRcvBuffer, &nRcvLen, FALSE);
	XStorageProtocol_Comm_ParseClient(tszRcvBuffer, tszMsgBuffer, &nMsgLen,&st_ProtocolHdr);
	m_EditMsg.SetWindowText(tszMsgBuffer);

	if (NETENGINE_COMMUNICATION_PROTOCOL_OPERATOR_CODE_AUTH_REPLOGIN == st_ProtocolHdr.unOperatorCode)
	{
		xhToken = st_ProtocolHdr.xhToken;
	}
}

void CTestStorageDlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	NETENGINE_PROTOCOL_USERAUTH st_UserAuth;

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	memset(&st_UserAuth, '\0', sizeof(NETENGINE_PROTOCOL_USERAUTH));

	_tcscpy(st_UserAuth.tszUserName, "123123aa");
	_tcscpy(st_UserAuth.tszPassword, "123123");
	st_UserAuth.enDeviceType = ENUM_PROTOCOL_FOR_DEVICE_TYPE_PC;
	st_UserAuth.enClientType = ENUM_PROTOCOL_FOR_SERVICE_TYPE_USER;

	XStorageProtocol_Client_REQLogin(tszMsgBuffer, &nMsgLen, &st_UserAuth);
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	NetClient_TCPSelect_Close(hSocket);
}


void CTestStorageDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQDirOperator(tszMsgBuffer, &nMsgLen, "aa");
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQDirQuery(tszMsgBuffer, &nMsgLen);
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQDirOperator(tszMsgBuffer, &nMsgLen, "file", FALSE);
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}

void CTestStorageDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	SOCKET m_Socket;
	CString m_StrIPAddr;
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	m_EditIPAddr.GetWindowText(m_StrIPAddr);

	if (!NetClient_TCPSelect_Create(m_StrIPAddr.GetBuffer(), 10332, &m_Socket))
	{
		AfxMessageBox(_T("连接失败!"));
		return;
	}
	LPCTSTR lpszFile = _T("G:\\netengineapp\\NetEngine_WINApps\\Debug\\123.exe");
	XStorageProtocol_Client_REQFile(tszMsgBuffer, &nMsgLen, lpszFile, "aa/", xhToken);
	NetClient_TCPSelect_SendMsg(m_Socket, tszMsgBuffer, nMsgLen);

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	NetClient_TCPSelect_RecvMsg(m_Socket, tszMsgBuffer, &nMsgLen, FALSE);
	OutputDebugString((tszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR)));

	NETENGINE_PROTOCOLHDR st_ProtocolHdr;
	memcpy(&st_ProtocolHdr, tszMsgBuffer, sizeof(NETENGINE_PROTOCOLHDR));


	FILE *pSt_File = _tfopen(lpszFile, "rb");
	while (1)
	{
		nMsgLen = 2048;
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		int nRet = fread(tszMsgBuffer, 1, 2048, pSt_File);
		if (nRet <= 0)
		{
			break;
		}
		while (1)
		{
			if (NetClient_TCPSelect_SendMsg(m_Socket, tszMsgBuffer, nMsgLen))
			{
				break;
			}
		}
		Sleep(10);
	}
	NetClient_TCPSelect_Close(m_Socket);
	fclose(pSt_File);
}
void CTestStorageDlg::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	SOCKET m_Socket;
	CString m_StrIPAddr;
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	m_EditIPAddr.GetWindowText(m_StrIPAddr);

	if (!NetClient_TCPSelect_Create(m_StrIPAddr.GetBuffer(), 10332, &m_Socket))
	{
		AfxMessageBox(_T("连接失败!"));
		return;
	}
	LPCTSTR lpszFile = _T("G:\\netengineapp\\NetEngine_WINApps\\Debug\\123.exe");
	XStorageProtocol_Client_REQFile(tszMsgBuffer, &nMsgLen, "TestFor_AuthRegClient.exe", "aa/", xhToken,FALSE);
	NetClient_TCPSelect_SendMsg(m_Socket, tszMsgBuffer, nMsgLen);

	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
	NetClient_TCPSelect_RecvMsg(m_Socket, tszMsgBuffer, &nMsgLen, FALSE);

	NETENGINE_PROTOCOLHDR st_ProtocolHdr;
	memcpy(&st_ProtocolHdr, tszMsgBuffer, sizeof(NETENGINE_PROTOCOLHDR));

	FILE *pSt_File = _tfopen(lpszFile, "wb");
	int nRecvCount = 0;
	int nWriteCount = 0;
	while (1)
	{
		nMsgLen = 2048;
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

		if (NetClient_TCPSelect_RecvMsg(m_Socket, tszMsgBuffer, &nMsgLen, FALSE))
		{
			nRecvCount += nMsgLen;
			int nRet = fwrite(tszMsgBuffer, 1, nMsgLen, pSt_File);
			nWriteCount += nRet;
			TCHAR tszPrint[64];
			memset(tszPrint, '\0', sizeof(tszPrint));
			_stprintf(tszPrint, "nRecvCount:%d nWriteCount:%d\n", nRecvCount, nWriteCount);
			OutputDebugString(tszPrint);
			if (nRecvCount >= 233472)
			{
				break;
			}
		}
	}
	NetClient_TCPSelect_Close(m_Socket);
	fclose(pSt_File);
}


void CTestStorageDlg::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQQueryFile(tszMsgBuffer, &nMsgLen,"20190701","20300101","TestFor_AuthRegClient.exe");
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnBnClickedButton10()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQDelete(tszMsgBuffer, &nMsgLen, "TestFor_AuthRegClient.exe");
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnBnClickedButton11()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQRegister(tszMsgBuffer, &nMsgLen, "aaadddzxc","123123");
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}


void CTestStorageDlg::OnBnClickedButton12()
{
	// TODO: 在此添加控件通知处理程序代码
	int nMsgLen = 2048;
	TCHAR tszMsgBuffer[2048];
	memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

	XStorageProtocol_Client_REQQueryUser(tszMsgBuffer, &nMsgLen, "123123aa", TRUE);
	NetClient_TCPSelect_SendMsg(hSocket, tszMsgBuffer, nMsgLen);
	Storage_MsgPrint();
}
